package io.renren.modules.biz.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.common.validator.Assert;
import io.renren.modules.biz.entity.CountDto;
import io.renren.modules.biz.entity.MemberEntity;
import io.renren.modules.biz.entity.TeamTreeDto;
import io.renren.modules.biz.service.MemberService;
import io.renren.modules.biz.service.TeamService;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

/**
 * 推广团队表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-09-03 14:51:31
 */
@RestController
@RequestMapping("biz/team")
@SuppressWarnings("unchecked")
public class TeamController {
	@Autowired
	private TeamService teamService;

	@Autowired
	private MemberService memberService;

	/**
	 * 伞下会员
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = teamService.selectMemberTeam(params);
		List<MemberEntity> list = (List<MemberEntity>) page.getList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (MemberEntity memberEntity : list) {
				if (StringUtils.isNotBlank(memberEntity.getRecPerson())) {
					MemberEntity entity = memberService.selectOne(new EntityWrapper<MemberEntity>().setSqlSelect("mobile").eq("code", memberEntity.getRecPerson()));
					memberEntity.setRecPerson(entity.getMobile());
				}
			}
		}
		return R.ok().put("page", page);
	}

	/**
	 * 本级
	 */
	@RequestMapping("/select")
	public R team(@RequestParam Map<String, Object> params) {
		String mobile = (String) params.get("mobile");
		String memberId = (String) params.get("memberId");
		Assert.isTrue(!(StringUtils.isBlank(mobile) && StringUtils.isBlank(memberId)), "请输入要查询的会员");
		MemberEntity entity = memberService.selectOne(new EntityWrapper<MemberEntity>().eq("mobile", mobile).or().eq("id", memberId));
		Assert.notNull(entity, "会员不存在");
		String startDate = (String) params.get("startDate");
		String endDate = (String) params.get("endDate");
		CountDto countDto = teamService.countTeam(entity.getId(), startDate, endDate);

		TeamTreeDto teamTreeDto = teamService.selectAccountingByMemberId(entity.getId(), startDate, endDate);
		return R.ok().put("countDto", countDto).put("accounting", teamTreeDto);
	}

	/**
	 * 下级列表
	 */
	@RequestMapping("/subList")
	public R subList(@RequestParam Map<String, Object> params) {
		String mobile = (String) params.get("mobile");
		String memberId = (String) params.get("memberId");
		Assert.isTrue(!(StringUtils.isBlank(mobile) && StringUtils.isBlank(memberId)), "请输入要查询的会员");
		MemberEntity entity = memberService.selectOne(new EntityWrapper<MemberEntity>().eq("mobile", mobile).or().eq("id", memberId));
		Assert.notNull(entity, "会员不存在");
		String startDate = (String) params.get("startDate");
		String endDate = (String) params.get("endDate");
		Integer level = Integer.valueOf(params.get("level").toString());
		Page<TeamTreeDto> pageInfo = new Query<TeamTreeDto>(params).getPage();
		PageUtils page = teamService.selectAccountingByHigher(pageInfo, level, entity.getId(), startDate, endDate);
		return R.ok().put("page", page);
	}
}
